import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

// Nikolas Wolfe
// CNT 4007
// Distance Algorithm

public class dvec
{
	private static final String networkData = "network.txt";
	
	public static void main(String[] args) throws IOException
	{
		ArrayList<String> textInfo;
		if (args.length < 1)
		{
			textInfo = readFile( networkData );
		}
		else {
			textInfo = readFile( args[0] );
		}
		List<Node> nodes = generateNodes(textInfo);
		for (Node i : nodes)
		{
			i.printAdjacencyTable();
		}
	}
	
	public static ArrayList<String> readFile( String filename ) throws IOException
	{
		FileInputStream input = new FileInputStream(filename);
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		ArrayList<String> textInfo = new ArrayList<String>();
		
		// input array
		String s = "";
		while( (s = reader.readLine()) != null )
		{
			if (s != "")
			{
				textInfo.add(s);
			}
		}
		reader.close();
		input.close();
		return textInfo;
	}
	
	private static List<Node> generateNodes(ArrayList<String> textInfo)
	{
		// number of nodes is in the first index
		int numNodes = Integer.parseInt(textInfo.remove(0));
		ArrayList<Node> list = new ArrayList<Node>();
		for( int i = 0; i < numNodes; ++i )
		{
			Node n = new Node();
			list.add(n);
			ArrayList<String> adjacencies = new ArrayList<String>();
			for( String s : textInfo )
			{
				String[] arr = s.split(",");
				if( Integer.parseInt(arr[0]) > i ) {
					break;
				}
				adjacencies.add( arr[1] + "," + arr[2] );
			}
			n.setAdjacencies(adjacencies);
		}
		for( Node n : list )
		{
			n.supplyNodes( list );
		}
		return list;
	}
}
